package com.greenold.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.greenold.common.annotation.Log;
import com.greenold.common.core.controller.BaseController;
import com.greenold.common.core.domain.AjaxResult;
import com.greenold.common.enums.BusinessType;
import com.greenold.service.domain.CareItem;
import com.greenold.service.service.ICareItemService;
import com.greenold.common.utils.poi.ExcelUtil;
import com.greenold.common.core.page.TableDataInfo;

/**
 * 护理项目Controller
 *
 * @author YueYang
 * @date 2025-10-14
 */
@RestController
@RequestMapping("/service/item")
public class CareItemController extends BaseController {
    @Autowired
    private ICareItemService careItemService;

    /**
     * 查询护理项目列表
     */
    @PreAuthorize("@ss.hasPermi('service:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(CareItem careItem) {
        startPage();
        List<CareItem> list = careItemService.selectCareItemList(careItem);
        return getDataTable(list);
    }

    /**
     * 查询所有可用的护理项目集合 也就是status为1的
     */
    // @PreAuthorize("@ss.hasPermi('service:item:list')")
    @GetMapping("/availableList")
    public AjaxResult availableList() {
        List<CareItem> list = careItemService.selectAvailableCareItemList();
        return AjaxResult.success(list);
    }


    /**
     * 获取护理项目详细信息 根据id获取回显数据
     */
    @PreAuthorize("@ss.hasPermi('service:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(careItemService.selectCareItemById(id));
    }

    /**
     * 查询护理计划项目关联表 获取与护理计划关联的所有护理项目id
     */
    //@PreAuthorize("@ss.hasPermi('service:item:query')")
    @GetMapping(value = "/relation")
    public AjaxResult getPlanItemList() {
        return success(careItemService.selectCareItemRelation());
    }

    /**
     * 新增护理项目
     */
    @PreAuthorize("@ss.hasPermi('service:item:add')")
    @Log(title = "护理项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CareItem careItem) {
        return toAjax(careItemService.insertCareItem(careItem));
    }

    /**
     * 修改护理项目
     */
    @PreAuthorize("@ss.hasPermi('service:item:edit')")
    @Log(title = "护理项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CareItem careItem) {
        return toAjax(careItemService.updateCareItem(careItem));
    }

    /**
     * 删除护理项目
     */
    @PreAuthorize("@ss.hasPermi('service:item:remove')")
    @Log(title = "护理项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(careItemService.deleteCareItemByIds(ids));
    }

//搜索服务项目在服务搜索里
    @GetMapping("/list1")
    public ResponseEntity<Map<String, Object>> listActiveCareItems() {
        List<CareItem> careItems = careItemService.getActiveCareItems();
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", careItems);
        return ResponseEntity.ok(result);
    }

}
